package com.cysyz.mylib.commonservice.statisticsupport.impl;

import java.util.List;

import com.cysyz.mylib.commonservice.servicesupport.CommonServiceSupport;
import com.cysyz.mylib.commonservice.statisticsupport.StatisticTimeAble;
import com.cysyz.mylib.commonservice.statisticsupport.StatisticTimeService;
import com.cysyz.mylib.dao.Conds;

public class StatisticTimeServiceImpl extends CommonServiceSupport implements StatisticTimeService {

	@Override
	public Integer searchLastRunDailyJobTimeSeconds(String statsticName) {
		Conds conds = new Conds();
		conds.addFilter("statistictime", "statisticName", statsticName);
		List<StatisticTimeAble> list = this.list(StatisticTimeAble.class, conds);
		Integer retval = null;
		if (list != null && !list.isEmpty()) {
			retval = list.get(0).getLastRunDailyjobSeconds();
		}
		return retval;
	}

	@Override
	public Integer searchLastDateAlreadyStatisticedSeconds(String statsticName) {
		Conds conds = new Conds();
		conds.addFilter("statistictime", "statisticName", statsticName);
		List<StatisticTimeAble> list = this.list(StatisticTimeAble.class, conds);
		Integer retval = null;
		if (list != null && !list.isEmpty()) {
			retval = list.get(0).getLastDateAlreadyStatisticSeconds();
		}
		return retval;
	}

	@Override
	public <T extends StatisticTimeAble> void saveLastDateAlreadyStatisticedSeconds(Class<T> clazz, String statsticName, Integer val) {
		Conds conds = new Conds();
		conds.addFilter("statistictime", "statisticName", statsticName);
		List<StatisticTimeAble> list = this.list(StatisticTimeAble.class, conds);
		if (list != null && !list.isEmpty()) {
			list.get(0).setLastDateAlreadyStatisticSeconds(val);
		} else {
			StatisticTimeAble statisticTime = null;
			try {
				statisticTime = clazz.newInstance();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			statisticTime.setStatisticName(statsticName);
			statisticTime.setLastDateAlreadyStatisticSeconds(val);
			this.save(statisticTime);
		}

	}

	@Override
	public <T extends StatisticTimeAble> void  saveLastRunDailyJobTimeSeconds(Class<T> clazz, String statsticName, Integer val) {
		Conds conds = new Conds();
		conds.addFilter("statistictime", "statisticName", statsticName);
		List<StatisticTimeAble> list = this.list(StatisticTimeAble.class, conds);
		if (list != null && !list.isEmpty()) {
			list.get(0).setLastRunDailyjobSeconds(val);
		} else {
			StatisticTimeAble statisticTime = null;
			try {
				statisticTime = clazz.newInstance();
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			statisticTime.setStatisticName(statsticName);
			statisticTime.setLastRunDailyjobSeconds(val);
			this.save(statisticTime);
		}
	}

}
